package com.zhongke.cotrun.controller;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.common.util.FileUpLoad;
import com.zhongke.cotrun.model.resultpackage.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.IOException;

@Api(tags = "文件上传")
@RestController
@Validated
public class Filecontroller {
    @Autowired
    FileUpLoad fileUpLoad;

    @ApiOperation(value = "文件上传", httpMethod = "POST", response = ReturnResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件名", required = true),
            @ApiImplicitParam(name = "model", value = "模块名", required = true),
    })
    @PostMapping("/upLoading")
    @RequiresUser
    public APIResponse upLoading(
            @NotNull(message = "文件名不能为空")
            @RequestParam("file") MultipartFile file,
            @NotNull(message = "模块名不能为空")String model,
            HttpServletRequest request) throws IOException {


        return APIResponse.returnSuccess(fileUpLoad.uploadFile(file, model, request));
    }
}
